package com.yiqi.liebang.feature.news.view;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseFragment;
import com.yiqi.liebang.R;
import com.yiqi.liebang.feature.news.view.adapter.ConversationListAdapterEx;
import com.yiqi.liebang.feature.news.view.adapter.MyPagerAdapter;
import com.yiqi.liebang.framework.api.UserApi;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.rong.imkit.RongContext;
import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imlib.model.Conversation;

/**
 * A simple {@link Fragment} subclass.
 */
public class MsgFragment extends BaseFragment {

    Unbinder unbinder;
    @BindView(R.id.tv_un_read)
    TextView mTvUnRead;
    @BindView(R.id.btn_msg_count)
    LinearLayout mBtnMsgCount;
    Unbinder unbinder1;
    private ConversationListFragment mConversationListFragment = null;


    private Conversation.ConversationType[] mConversationsTypes = null;

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_msg;
    }

    @Override
    protected void initInjector() {

    }


    @Override
    protected void initView() {
        super.initView();

        if (!initConversationList().isAdded()) {
            Fragment videoFragment = initConversationList();
            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            transaction.add(R.id.rong_msg_list, videoFragment).commit();
        }

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {

    }


    @Override
    public void onStart() {
        super.onStart();
        getUnRead();
    }


    private Fragment initConversationList() {
        if (mConversationListFragment == null) {
            ConversationListFragment listFragment = new ConversationListFragment();
            listFragment.setAdapter(new ConversationListAdapterEx(RongContext.getInstance()));
            Uri uri;
            if (AM.isDebug()) {
                uri = Uri.parse("rong://" + AM.app().getPackageName()).buildUpon()
                        .appendPath("conversationlist")
                        .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话是否聚合显示
                        .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "mIvUserPosition")//群组
                        .appendQueryParameter(Conversation.ConversationType.PUBLIC_SERVICE.getName(), "false")//公共服务号
                        .appendQueryParameter(Conversation.ConversationType.APP_PUBLIC_SERVICE.getName(), "false")//订阅号
                        .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "true")//系统
                        .appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "true")
                        .build();
                mConversationsTypes = new Conversation.ConversationType[]{Conversation.ConversationType.PRIVATE,
                        Conversation.ConversationType.GROUP,
                        Conversation.ConversationType.PUBLIC_SERVICE,
                        Conversation.ConversationType.APP_PUBLIC_SERVICE,
                        Conversation.ConversationType.SYSTEM,
                        Conversation.ConversationType.DISCUSSION
                };

            } else {
                uri = Uri.parse("rong://" + AM.app().getPackageName()).buildUpon()
                        .appendPath("conversationlist")
                        .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话是否聚合显示
                        .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "false")//群组
                        .appendQueryParameter(Conversation.ConversationType.PUBLIC_SERVICE.getName(), "false")//公共服务号
                        .appendQueryParameter(Conversation.ConversationType.APP_PUBLIC_SERVICE.getName(), "false")//订阅号
                        .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "true")//系统
                        .build();
                mConversationsTypes = new Conversation.ConversationType[]{Conversation.ConversationType.PRIVATE,
                        Conversation.ConversationType.GROUP,
                        Conversation.ConversationType.PUBLIC_SERVICE,
                        Conversation.ConversationType.APP_PUBLIC_SERVICE,
                        Conversation.ConversationType.SYSTEM
                };
            }
            listFragment.setUri(uri);
            mConversationListFragment = listFragment;
            return listFragment;
        } else {
            return mConversationListFragment;
        }
    }


    @OnClick(R.id.btn_msg_count)
    public void onViewClicked() {
        startActivity(new Intent(getActivity(), PendingIssuesActivity.class));
    }

    private void getUnRead() {
        AM.api().createApiService(UserApi.class)
                .getUserTreatedNum()
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {
                        int num = Integer.valueOf(s);
                        if (mTvUnRead != null) mTvUnRead.setText(num + "个请求待处理事项");
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
